import { Person } from "../model/person.js"
import { Op } from "sequelize"


const add = async (ctx)=> {
    let data = ctx.request.body
    Person.create(data)
}


const get_list = async (ctx)=> {
    let keyword = ctx.request.query.keyword||""
    let datas =await Person.findAll({
        where:{
            name:{
                [Op.like]: `%${keyword}%`
            }
        }
    })
    ctx.body=datas

}


const getbyid = async (ctx)=>{
    let id = ctx.params.id||0
    let data =await Person.findByPk(id)
    ctx.body=data
    
}


const del = async (ctx)=> {
    let id = ctx.params.id||0
    console.log(id);
    Person.destroy({
        where:{
            id:id
        }
    })
}


const edit = async (ctx)=> {
    let id = ctx.params.id || 0
    let data = ctx.request.body
    Person.update(data,{
        where:{
            id:id
        }
    })
}



export default {
    add,
    get_list,
    getbyid,
    del,
    edit
}